<?php
class Cms_Model_Subscriber
{
	private static $_instance;
	private static $_cacheTimeout = 300;
	
	private function __construct() {
	}
	
	/**
	 * @return Cms_Model_Subscriber
	 * */
	public static function factory() {
		if(self::$_instance == null) {
			self::$_instance = new self();
		}
		
		return self::$_instance;
	}
	
	/**
	 * Insert new subscriber with provided information
	 * @param array $data
	 * @return true on success else false
	 * */
	public function insert($data) {
	    $result = 0;
	    try {
	        $dao = Cms_Model_DAO_Subscriber::factory();
	        $result = $dao->insert($data);
	    } catch (Exception $exc) {
	        if($exc->getCode() == 23000) {
	            $email = $data['email'];
	            unset($data['email']);
	            $result = $dao->update($email, $data);
	            if(!$result) {
	                $result = -1;
	            }
	        }
// 	        prBinh($exc);    
	        Utils_Global::storeLog($exc, __FILE__, __LINE__);
	    }
	    
	    return $result;
	}
	
	/**
	 * Update subscriber
	 * @param string email
	 * @param array $data
	 * @return boolean
	 * */
	public function update($email, $data) {
	    $result = 0;
	    if(!$email) {
	        return $result;
	    }
	    try {
	        $dao = Cms_Model_DAO_Subscriber::factory();
	        $result = $dao->update($email, $data);
	    } catch (Exception $exc) {
	        prBinh($exc);
	        Utils_Global::storeLog($exc, __FILE__, __LINE__);
	    }
	    
	    return $result;
	}
}